// https://leetcode.cn/problems/implement-trie-prefix-tree/description/?envType=study-plan-v2&envId=top-interview-150

class Node {
public:
    unordered_map<char, Node*> children;
    bool is_end;
    Node() : is_end(false) { }
};

class Trie {
private:
    Node* root;
public:
    Trie() {
        root = new Node();
    }
    
    void insert(string word) {
        Node* cur = root;
        for (char ch : word) {
            if (!cur->children.count(ch)) {
                Node* newNode = new Node();
                cur->children[ch] = newNode;
            } 
            cur = cur->children[ch];
        }
        cur->is_end = true;
    }
    
    bool search(string word) {
        Node* cur = root;
        for (char ch : word) {
            if (!cur->children.count(ch)) {
                return false;
            }
            cur = cur->children[ch];
        }
        return cur->is_end;
    }
    
    bool startsWith(string prefix) {
        Node* cur = root;
        for (char ch : prefix) {
            if (!cur->children.count(ch)) {
                return false;
            }
            cur = cur->children[ch];
        }
        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */